<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-dialogDrag
      :visible.sync="visible">
      <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"
               :disabled="method==='view'"
               label-width="120px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="用户ID" prop="user.id"
                          :rules="[
                 ]">
              <GridSelect
                title="选择用户ID"
                labelName='phone'
                labelValue='id'
                :value="inputForm.user.id"
                :limit="1"
                @getValue='(value) => {inputForm.user.id=value}'
                :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'phone',
              label: '手机号(账号唯一)'
            },
            {
              prop: 'image',
              label: '头像'
            },
            {
              prop: 'nikeName',
              label: '昵称'
            }
            ]"
                :searchs="[
            {
              prop: 'phone',
              label: '手机号(账号唯一)'
            },
            {
              prop: 'nikeName',
              label: '昵称'
            }
            ]"
                dataListUrl="/userinfo/tUserInfo/list"
                entityBeanName="tUserInfo"
                queryEntityUrl="/userinfo/tUserInfo/queryById">
              </GridSelect>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="活动名称" prop="activityName"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.activityName" placeholder="请填写活动名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="礼品ID" prop="giftId"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.giftId" placeholder="请填写礼品ID"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="礼品名称" prop="giftName"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.giftName" placeholder="请填写礼品名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="礼品图片" prop="giftImage"
                          :rules="[
                 ]">
              <el-upload ref="giftImage"
                         v-if="visible"
                         list-type="picture-card"
                         :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/commonexchangerecords/commonExchangeRecords`"
                         :headers="{token: $cookie.get('token')}"
                         :on-preview="(file, fileList) => {
                        $alert(`<img style='width:100%' src=' ${(file.response && file.response.url) || file.url}'/>`,  {
                          dangerouslyUseHTMLString: true,
                          showConfirmButton: false,
                          closeOnClickModal: true,
                          customClass: 'showPic'
                        });
                    }"
                         :on-success="(response, file, fileList) => {
                       inputForm.giftImage = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                         :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.giftImage = fileList.map(item => item.url).join('|')
                    }"
                         :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                         multiple
                         :limit="5"
                         :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                         :file-list="giftImageArra">
                <i class="el-icon-plus"></i>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="兑换时间" prop="redemptionTime"
                          :rules="[
                 ]">
              <el-date-picker
                style="width: 100%;"
                v-model="inputForm.redemptionTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="兑换方式" prop="exchangeMethod">
              <el-radio-group v-model="inputForm.exchangeMethod">
                <el-radio label="1">线下领取</el-radio>
                <el-radio label="2">快递邮寄</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收货人" prop="consignee"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.consignee" placeholder="请填写收货人"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phoneNumber"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.phoneNumber" placeholder="请填写手机号码"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所在区域" prop="region"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.region" placeholder="请填写所在区域"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="详细地址" prop="detailedAddress"
                          :rules="[
                 ]">
              <el-input v-model="inputForm.detailedAddress" placeholder="请填写详细地址"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="兑换状态" prop="redemptionStatus"
                          :rules="[
                 ]">
              <el-radio-group v-model="inputForm.redemptionStatus">
                <el-radio label="0">未兑换</el-radio>
                <el-radio label="1">已兑换</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                          :rules="[
                 ]">
              <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
import GridSelect from '@/components/gridSelect'

export default {
  data() {
    return {
      title: '',
      method: '',
      visible: false,
      loading: false,
      giftImageArra: [],
      inputForm: {
        id: '',
        user: {
          id: ''
        },
        activityName: '',
        giftId: '',
        giftName: '',
        giftImage: '',
        redemptionTime: '',
        exchangeMethod: '',
        consignee: '',
        phoneNumber: '',
        region: '',
        detailedAddress: '',
        redemptionStatus: '',
        remarks: ''
      }
    }
  },
  components: {
    GridSelect
  },
  methods: {
    init(method, id) {
      this.method = method
      this.inputForm.id = id
      if (method === 'add') {
        this.title = `新建兑换记录信息`
      } else if (method === 'edit') {
        this.title = '修改兑换记录信息'
      } else if (method === 'view') {
        this.title = '查看兑换记录信息'
      }
      this.giftImageArra = []
      this.visible = true
      this.loading = false
      this.$nextTick(() => {
        this.$refs.inputForm.resetFields()
        if (method === 'edit' || method === 'view') { // 修改或者查看
          this.loading = true
          this.$http({
            url: `/commonexchangerecords/commonExchangeRecords/queryById?id=${this.inputForm.id}`,
            method: 'get'
          }).then(({data}) => {
            this.inputForm = this.recover(this.inputForm, data.commonExchangeRecords)
            this.inputForm.giftImage.split('|').forEach((item) => {
              if (item.trim().length > 0) {
                this.giftImageArra.push({
                  name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)),
                  url: item
                })
              }
            })
            this.loading = false
          })
        }
      })
    },
    // 表单提交
    doSubmit() {
      this.$refs['inputForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$http({
            url: `/commonexchangerecords/commonExchangeRecords/save`,
            method: 'post',
            data: this.inputForm
          }).then(({data}) => {
            this.loading = false
            if (data && data.success) {
              this.visible = false
              this.$message.success(data.msg)
              this.$emit('refreshDataList')
            }
          })
        }
      })
    }
  }
}
</script>
